package com.pdf.pdfserver.service.solr;

import com.pdf.pdfserver.util.SolrDocumentUtil;
import com.pdf.pdfserver.vo.PageVO;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author songfz
 * @Description
 * @Date 2022/1/11
 */
public class BaseSolrServer<T> implements SolrService<T> {

    @Autowired
    private SolrClient solrClient;

    private Class<T> clazz;

    public BaseSolrServer() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) pt.getActualTypeArguments()[0];
    }

    @Override
    public PageVO<T> search(String keyword, String db_core,Pageable pageable) throws IOException, SolrServerException {
        SolrQuery params = new SolrQuery();
        params.setStart(pageable.getPageNumber()-1);
        params.setRows(pageable.getPageSize());
        params.set("mm","80%");
        params.set("q","keyword:" + keyword);
        QueryResponse queryResponse  = solrClient.query(db_core,params);
        SolrDocumentList results = queryResponse.getResults();
        long numFound = results.getNumFound();
        List<T> tList = results.stream()
                .filter(result -> Objects.nonNull(result))
                .map(result -> SolrDocumentUtil.convert(result, clazz))
                .collect(Collectors.toList());

        return new PageVO<T>( pageable.getPageSize(),pageable.getPageNumber(), tList, Long.valueOf(numFound),Long.valueOf(numFound)/pageable.getPageSize()+1);
    }

    @Override
    public Boolean add(String dbCore, T entity) throws IOException, SolrServerException {

        UpdateResponse response = solrClient.addBean(dbCore,entity);
        solrClient.commit(dbCore);
        return response.getStatus() == 0;
    }

    @Override
    public boolean addList(String dbCore, List<T> list) throws IOException, SolrServerException {

        UpdateResponse response = solrClient.addBeans(dbCore, list);
        return response.getStatus() == 0;
    }

    @Override
    public T getById(String dbCore, String id) throws IOException, SolrServerException {
        return SolrDocumentUtil.convert(solrClient.getById(dbCore,id),clazz);
    }

    @Override
    public boolean deleteById(String dbCore, String id) throws IOException, SolrServerException {
        UpdateResponse response = solrClient.deleteById(dbCore,id);
        solrClient.commit(dbCore);
        return response.getStatus() == 0;
    }

    @Override
    public boolean deleteByString(String dbCore, String query) throws IOException, SolrServerException {


        UpdateResponse response =  solrClient.deleteByQuery(dbCore,query);
        solrClient.commit(dbCore);
        return response.getStatus() == 0;
    }


}
